import urllib.request
import re
import http.cookiejar
import gzip
import http.cookiejar
import codecs
def ungzip(data):
	try:
		print('正在解压...')
		data=gzip.decompress(data)
		print('解压完毕')
	except:
		print('解压失败')
	return data
def get_xsrf(data):
	#print(data.encode('utf8'))
	cer=re.compile('name=\"_xsrf\" value=\"(.+)\"',flags=0)
	strlist=cer.findall(data)
	return strlist[0]
def getOpener(head):
	cj=http.cookiejar.CookieJar()
	opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
	header=[]
	for key,value in head.items():
		elem=(key,value)
		header.append(elem)
	opener.addheaders=header
	return opener

header={
	'connection':'Keep-Alive',
	'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
	'Upgrade-Insecure-Requests':'1',
	'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.3',
	#'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko',
	'Accept-Encoding':'gzip, deflate, sdch',
	'Accept-Language':'zh-CN,zh;q=0.8'
}

url="https://www.zhihu.com"
opener=getOpener(header)
op=opener.open(url)
data=op.read()
data=ungzip(data)
_xsrf=get_xsrf(data.decode('utf8'))
print(_xsrf.encode('utf8'))
url=url+'/login/email'
id='用户的邮箱账号'
passwd='密码'
postDict={
	'_xsrf':_xsrf,
	'email':id,
	'password':passwd,
	'rememberme':'y'
}
data=urllib.parse.urlencode(postDict).encode('utf8')
op=opener.open(url,data)
data=op.read()
data=ungzip(data)


file=codecs.open('zhihu.html','w')
file.write(data.decode('utf8'))
file.close()
print(data.decode('utf8'))
